﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using DTO;
using DAO;
using BUS;
namespace Cucre
{
    public partial class danhsachdonhang : System.Web.UI.Page
    {
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            string path = Server.MapPath("theme.txt");
            StreamReader SR = new StreamReader(path);
            string nameTheme = SR.ReadLine();
            SR.Close();
            base.OnPreInit(e);
            if (Session["MyTheme"] == null)
            {
                Session.Add("MyTheme", nameTheme);
                Page.Theme = (string)Session["MyTheme"];
            }
            else
            {
                Page.Theme = (string)Session["MyTheme"];
            }
        }

        public DataClasses1DataContext Data = new DataClasses1DataContext();

       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TinKhuyenMai_DataBind();
            }
        }
        public void TinKhuyenMai_DataBind()
        {
            PROVIDER Provider = (PROVIDER)Session["user_login"];
            var query = from product in Data.PRODUCTs
                        where ( (product.VISIBLE == true) && (product.IDPROVIDER==Provider.IDPROVIDER))
                        select new
                        {
                            IDPRODUCT = product.IDPRODUCT,
                            Name_Product = product.Name_Product,
                            TIME_START = product.TIME_START.Value.ToShortDateString(),
                            TIME_STOP = product.TIME_STOP.Value.ToShortDateString(),
                            PRICE = product.PRICE,
                            RATE_DECREASE=product.RATE_DECREASE.ToString() + "%",
                            CITY = product.CITY,
                            TOTAL_ORDER=Data.ORDERs.Count(order =>order.IDPRODUCT == product.IDPRODUCT),
                            TOTAL_MONEY=(from order in Data.ORDERs where (order.IDPRODUCT == product.IDPRODUCT) select order.PAYMENT).Sum()
                        };
            TinKhuyenMai_GridView.DataSource = query;
            TinKhuyenMai_GridView.DataBind();
        }

        protected void TinKhuyenMai_GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            TinKhuyenMai_GridView.SelectedIndex = e.NewSelectedIndex;
            GridView_ListOrder_DataBind();
        }

        protected void TinKhuyenMai_GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            TinKhuyenMai_GridView.PageIndex = e.NewPageIndex;
        }

        protected void GridView_ListOrder_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_ListOrder.PageIndex = e.NewPageIndex;
        }

        public void GridView_ListOrder_DataBind()
        {
            if (TinKhuyenMai_GridView.SelectedIndex >= 0)
            {
                int idProduct = int.Parse(TinKhuyenMai_GridView.Rows[TinKhuyenMai_GridView.SelectedIndex].Cells[0].Text.Trim());
                var query = from order in Data.ORDERs
                            where (order.IDPRODUCT == idProduct)
                            select new
                            {
                                IDORDER=order.IDORDER,
                                QUATITY=order.QUANTITY,
                                PAYMENT=order.PAYMENT,
                                TIMEORDER=order.TIME_ORDER.Value.ToShortDateString(),
                                STATUS=order.STATUS_ORDER,
                                CUSTOMER=order.MEMBER.ACCOUNT.FULLNAME,
                                EMAIL=order.MEMBER.ACCOUNT.EMAIL,
                                CMND=order.MEMBER.ACCOUNT.CMND,
                                ADDRESS=order.MEMBER.ACCOUNT.ADDRESS
                            };
                GridView_ListOrder.DataSource = query;
                GridView_ListOrder.DataBind();
            }
        }

       

       


    }
}
